define([
	"UtilDir/util",
	"UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
	"http://api.tianditu.gov.cn/api?v=4.0&tk=aeb17a8215786a5dd17f5f9c92bebc0c",
	"Date","DateCN", "css!DateCss",
	"EasyUI","EasyUI-lang"
    ],function(Util, Tool, SelectOrg, SearchBlock){

    var map = null;

    var sysPath =  getServer() + "/static/app/cdqh/fourthgenerations";

    var qhGeoFourthgenerationsFormHtml = sysPath + "/views/qhGeoFourthgenerationsForm.html";
    var getQhGeoFourthgenerationsController = function(){
        return getServer() + "/qh/qhGeoFourthgenerationss";
    };

	var getQhGeoFourthgenerationssController = function(){
		return getServer() + "/qh/qhGeoFourthgenerationss";
	};

    var formValidator = function(){
        $("#qhGeoFourthgenerationsForm").validate({
            rules : {
                shape : {
                    required : true,
                },
                acc : {
                    required : true,
                },
            },
            messages : {
                shape : {
                    required : "区域不允许为空!",
                },
                acc : {
                    required : "累加类型不允许为空!",
                },
            }
        });
    };
    var getQhGeoFourthgenerations = function(id){
        $.ajax({
            url: getQhGeoFourthgenerationssController() + "/"+id,
            type: "get",
            success: function (data) {
                Tool.deserialize("qhGeoFourthgenerationsForm", data);
            }
        });
    };

    window._editQhGeoFourthgenerations = function(qhGeoFourthgenerationsId) {
        var slidebar = Util.slidebar({
            url: qhGeoFourthgenerationsFormHtml,
            width: "580px",
            cache: false,
            close : true,
            afterLoad: function () {
                formValidator();
                getQhGeoFourthgenerations(qhGeoFourthgenerationsId);
                $("#saveBtn").on("click", function () {
                    if($("#qhGeoFourthgenerationsForm").valid()){
                        var data = Tool.serialize("qhGeoFourthgenerationsForm");
                        $.ajax({
                            url: getQhGeoFourthgenerationsController(),
                            contentType:"application/json",
                            data: JSON.stringify(data),
                            type: "put",
                            success: function (data) {
                                Util.alert(data.message);
                                slidebar.close();
                                // createQhGeoFourthgenerationsGrid();
                            }
                        });
                    }
                });
            }
        });
    };

	/**
	 * 页面初始化
	 */
	var init = function(){
		initMap();
        showData();
	};

    var showData = function () {
        $.ajax({
            url: getQhGeoFourthgenerationssController() + "/queryQhGeoFourthgenerationssNoPage",
            contentType:"application/json",
            type: "post",
            data:JSON.stringify({
            }),
            success: function (res) {
                debugger;
                var features = res.data;
                if(features.length < 1) {
                    return;
                }

                //features.reverse();
                
                features.forEach(function (feature) {
                    var shapeStr = feature.shape;
                    var pointsStr = shapeStr.substring(shapeStr.indexOf("((") + 2,shapeStr.indexOf("))"));
                    var pointRegions = pointsStr.split("),(");
                    pointRegions.forEach(function (pointRegion) {
                        var pointList = pointRegion.split(",");
                        var points = [];
                        pointList.forEach(function (pointStr) {
                            var pointCoord = pointStr.split(" ");

                            var lon = parseFloat(pointCoord[0]);
                            var lat = parseFloat(pointCoord[1]);
                            if(isNaN(lon) || isNaN(lat) || lon > 180 || lon < -180 || lat > 90 || lat < -90) {
                                return;
                            }

                            points.push(new T.LngLat(lon, lat))
                        });
                        if(!points[0].equals(points[points.length - 1])){
                            points.push(points[0]);
                        }


                        var fillColor = "#FFFFFF";
                        switch (feature.acc) {
                            case 25:{
                                fillColor = "#F5F4EE";
                                break;
                            }
                            case 50:{
                                fillColor = "#FAECE1";
                                break;
                            }
                            case 100:{
                                fillColor = "#FAE4D4";
                                break;
                            }
                            case 150:{
                                fillColor = "#FADCCF";
                                break;
                            }
                            case 200:{
                                fillColor = "#FACA77";
                                break;
                            }
                            case 300:{
                                fillColor = "#FAB297";
                                break;
                            }
                            case 400:{
                                fillColor = "#DA7A77";
                                break;
                            }
                            default:{
                            }
                        }

                        //创建面对象
                        var polygon = new T.Polygon(points, {
                            color: "#D8D1CB",
                            weight: 3,
                            opacity: 0.5,
                            fillColor: fillColor,
                            fillOpacity: 0.5
                        });
                        polygon.fid = feature.ogrFid;
                        polygon.acc = feature.acc;
                        polygon.lonlat = points[0];
                        //向地图上添加面
                        map.addOverLay(polygon);

                        polygon.addEventListener("click", polygonClick);
                    })

                })
            }
        });
	};

    var polygonClick = function (e) {
        _editQhGeoFourthgenerations(this.fid);
    };

	var initMap = function() {
        //初始化地图对象
        var config = {
            projection: "EPSG:4326",
            minZoom: 4
        };

        map = new T.Map("mapDiv",config);
        //设置显示地图的中心点和级别
        map.centerAndZoom(new T.LngLat(116.40969,39.89945),5);

        var control=new T.Control.Zoom();
        map.addControl(control);

        //创建比例尺控件对象
        var scale = new T.Control.Scale();
        map.addControl(scale);

        //创建鹰眼控件对象
        var miniMap = new T.Control.OverviewMap({
            isOpen: true,
            size: new T.Point(150, 150)
        });
        map.addControl(miniMap);

        //创建地图类型控件对象
        var ctrl = new T.Control.MapType();
        map.addControl(ctrl);

        var height = window.innerHeight - 15;
        var width = window.innerWidth - 15;

        $("#mapDiv").css("height",height);
        $("#mapDiv").css("width",width);

        map.checkResize();
	};
	return {
		init:init
	};
});
